<?php
/**
 * Created by PhpStorm.
 * User: Jesse
 * Date: 2018/3/1
 * Time: 18:33
 */
namespace App\Admin\Extensions\Tools;

use Encore\Admin\Admin;
use Encore\Admin\Grid\Tools\AbstractTool;

class makeCategoryLetter extends AbstractTool
{
    public function script()
    {
        $url = '/' . config('admin.route.prefix') . '/category/makeCategoryLetter';
        return <<<EOT

$('.form-ajax-button').on('click', function() {
    var name = $('#name').val();
    if (name == '') {
        toastr.error('栏目名称不能为空');
        return false;
    }

    $.ajax({
        method: 'get',
        url: '{$url}',
        data: {name:name},
        success: function (res) {
            if (res.code == 1) {
                $('#letter').val(res.data.slug);
                toastr.success(res.msg);
            } else {
                toastr.error(res.msg);
            }
        }
    });
});

EOT;
    }

    public function render()
    {
        Admin::script($this->script());

        return "<a class=\"btn btn-twitter btn-xs form-ajax-button\">生成栏目拼音</a>";
    }
}